import getpath
import os
import re
import logging
import copy
import xml.etree.ElementTree
import myglobal
import openpyxl
from openpyxl.drawing.image import Image
from PyQt5 import QtWidgets
import io
from PIL import Image as PILImage

doc=None
filename=None
curvePage=-1
def elementWave(s):
    print(s)
    find=re.search(r"\d+(\.\d+)?[A-Z][a-z]*", s).group()
    wave=re.search(r"\d+(\.\d+)?", find).group()
    element=re.search(r"[A-Z][a-z]*", find).group()
    mode=""#s.split("(")[1].split(")")[0].strip().replace(" ","").replace("-","_")
    return(element,wave,True,mode)
def getdata_raw(fname):
    from openpyxl import load_workbook
    xl_book = load_workbook(filename=fname)
    table = xl_book.worksheets[0]
    data = []
    for row in table.values:
        r = []
        for one in row:
            if one is not None:
                r.append(one)
            else:
                r.append("")
        data.append(r)
    table = xl_book.worksheets[2]
    data2 = []
    for row in table.values:
        r = []
        for one in row:
            if one is not None:
                r.append(one)
            else:
                r.append("")
        data2.append(r)
    return (data,data2)
def zl_data(data,data2):
    start=2
    data_ele=data[0][start:]
    at=0
    items=[]
    concs=[]
    colidx=[]
    while(at<len(data_ele)):
        fs=data_ele[at].split(" ")
        r=elementWave(data_ele[at])
        items.append(r)
        colidx.append(at+start)
        at+=1
    print(items)
    data=data[1:]
    data2=data2[1:]
    r=[]
    cps=[]
    for i in range(len(data)):
        sample={}
        # sample["items"]=copy.copy(items)
        row=data[i]
        row_cps=data2[i]
        id1=row[1]
        if id1=="":
            continue
        sample["sampleid"]=str(id1)
        sample["time"]=""
        sample["mass"]=""
        sample["volume"]=""
        concs=[]
        cps_row=[]
        for j in range(len(items)):
            v=row[colidx[j]]
            v_cps=row_cps[colidx[j]]
            print(v)
            if v==None:
                concs.append("")
            else:
                v=str(v)
                arr=v.split("%")
                if len(arr)==1:
                    concs.append(v)
                else:
                    print("%")
                    print("i==0")
                    if i==0:
                        k=items[j]
                        items[j]=(k[0]+"内标",k[1],False,k[3])
                    concs.append(arr[0])
            if v_cps==None:
                cps_row.append("")
            else:
                cps_row.append(v_cps)
        sample["conc"]=concs
        cps.append(cps_row)
        r.append(sample)
    print(cps)
    items2=[(one[0]+"CPS",one[1],False,one[3]) for one in items]
    items+=items2
    for i in range(len(r)):
        r[i]["conc"]+=cps[i]
    return (items,r)
def getdata(fname):
    (data0,data1)=getdata_raw(fname)
    data=zl_data(data0,data1)
    return data
def test_getdata():
    fname=os.path.join(getpath.getpath(),"..","data","ncs300","002.xlsx")
    data=getdata(fname)
    return data

if __name__=="__main__":
    data=test_getdata()
    print(data)
    # for one in data:
    #     print(one["id"])
    # print(data[0])
    # print(data[1])
    # print(data[-2])
    # print(data[-1])
